package com.video.zlc.audioplayer.ui;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.video.zlc.audioplayer.R;
import com.video.zlc.audioplayer.adapter.VoiceListAdapter;
import com.video.zlc.audioplayer.info.FendaListInfo;
import com.video.zlc.audioplayer.utils.JsonUtil;
import com.video.zlc.audioplayer.utils.LogUtil;
import com.video.zlc.audioplayer.business.MediaBusiness;
import com.video.zlc.audioplayer.utils.ParamsUtil;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by zlc on 2016/6/18.
 */
public class ListenListActivity extends BaseActivity implements VoiceListAdapter.OnClickListener{

    private ListView list_listen;
    //分答信息相关的
    private VoiceListAdapter mAdapter;
    private List<FendaListInfo.ObjsEntity> objs;
    private ImageView im_fanhui;
    private TextView title;

    private ImageView id_iv_voice;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        initData();
        initListener();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wenda;
    }


    protected void initView() {
        im_fanhui = findView(R.id.id_iv_fanhui);
        title = findView(R.id.id_tv_title);
        list_listen = findView(R.id.list_listen);
        list_listen = findView(R.id.list_listen);
        title.setText("健康热听列表");
    }

    protected void initData() {
        objs = new ArrayList<>();
        getFendaInfo();
    }

    private void getFendaInfo() {
        LogUtil.e("getFendaInfo",ParamsUtil.getVoiceList());
        FendaListInfo info = JsonUtil.parseJsonToBean(ParamsUtil.getVoiceList(), FendaListInfo.class);
        if(info==null){
            LogUtil.e("分答列表info","JSON解析失败");
        }else if("1".equals(info.status)) {
            setFendaListInfo(info);
        }
    }

    private void setFendaListInfo(FendaListInfo info) {

        if(info.body!=null && info.body.size() > 0) {
            objs = info.body.get(0).objs;
            list_listen.setAdapter( mAdapter = new VoiceListAdapter(ListenListActivity.this,objs));
        }
        mAdapter.setOnClickListener(this);
    }

    protected void initListener() {
        im_fanhui.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.id_iv_fanhui:
                finish();
                break;
        }
    }

    @Override
    public void onClick(View v, ImageView iv_voice,int position) {
       if(position < objs.size()){
           FendaListInfo.ObjsEntity obj = objs.get(position);
           id_iv_voice = iv_voice;
           if(obj!=null)
               playVoice(obj,position);
       }
    }

    private int lastPosition = -1;
    private void playVoice(FendaListInfo.ObjsEntity obj, int position) {
        String videoPath = obj.path;
        if(TextUtils.isEmpty(videoPath) || !videoPath.contains(".mp3")){
            Toast.makeText(this,"语音文件不合法",Toast.LENGTH_LONG).show();
            return;
        }
        if(position != lastPosition){  //点击不同条目先停止动画 重置音频资源
            stopAnimation();
            MediaBusiness.reset();
        }
        if(mAdapter!=null)
            mAdapter.selectItem(position, lastPosition);
        lastPosition = position;

        id_iv_voice.setBackgroundResource(R.drawable.animation_voice);
        animationDrawable = (AnimationDrawable) id_iv_voice.getBackground();
        if(MediaBusiness.isPlaying()){
            stopAnimation();
            MediaBusiness.pause();
        }else if(MediaBusiness.isPause()){
            startAnimation();
            MediaBusiness.resume();
        }else{
            startAnimation();
            MediaBusiness.playSound(this,videoPath, new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    LogUtil.e("onCompletion","播放完成");
                    stopAnimation();
                    MediaBusiness.stop();
                }
            });
        }
    }

    private void startAnimation(){
        if(id_iv_voice == null)
            return;
        id_iv_voice.setBackgroundResource(R.drawable.animation_voice);
        animationDrawable = (AnimationDrawable) id_iv_voice.getBackground();
        if (animationDrawable != null && !animationDrawable.isRunning()) {
            animationDrawable.start();
        }
    }

    private void stopAnimation(){
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
            id_iv_voice.setBackgroundResource(R.drawable.voice_listen);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopAnimation();
        MediaBusiness.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopAnimation();
        if(MediaBusiness.isPlaying()){
            MediaBusiness.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        startAnimation();
        if(MediaBusiness.isPause()){
            MediaBusiness.resume();
        }
    }
}
